package com.chenglin.mapdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chenglin.mapdemo.R;
import com.chenglin.mapdemo.bean.LayerBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 内部RecyclerView的adapter
 */
public class LayerInnerAdapter extends RecyclerView.Adapter<LayerInnerAdapter.MyHolder> {

    private Context context;
    private List<LayerBean.InnerBean> list;
    private InnerLayerCallBack callBack;

    public LayerInnerAdapter(Context context, List<LayerBean.InnerBean> list, InnerLayerCallBack callBack) {
        this.context = context;
        this.list = list;
        this.callBack = callBack;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layer_inner_adapter, parent, false);
        return new MyHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.tvTypeName.setText(list.get(position).getInner_name());
        Glide.with(context).load(list.get(position).getUrl()).into(holder.ivType);
        if (list.get(position).getSelect()) {
            holder.ivSelect.setVisibility(View.VISIBLE);
            holder.ivSelectMark.setVisibility(View.VISIBLE);
        } else {
            holder.ivSelect.setVisibility(View.GONE);
            holder.ivSelectMark.setVisibility(View.GONE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onItemClick(list.get(position), position);

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_type)
        ImageView ivType;
        @BindView(R.id.iv_select)
        ImageView ivSelect;
        @BindView(R.id.iv_select_mark)
        ImageView ivSelectMark;
        @BindView(R.id.tv_type_name)
        TextView tvTypeName;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface InnerLayerCallBack {
        void onItemClick(LayerBean.InnerBean bean, int innerPosition);
    }
}
